import axios from '@/utils/request.js'
//所有频道
export const getAllChannelsAPI = () => {
  return axios({
    url: '/v1_0/channels',
    method: 'GET'
  })
}

export const loginAPI = ({ mobile, code }) =>
  axios({
    url: '/v1_0/authorizations',
    method: 'POST',
    data: {
      mobile,
      code
    }
  })

export const getListNewsAPI = ({ channel_id, timestamp }) =>
  axios({
    url: '/v1_0/articles',
    params: {
      channel_id,
      timestamp
    }
  })
//用户已选的频道
export const getUserChannelsAPI = () =>
  axios({
    url: '/v1_0/user/channels',
    method: 'GET'
  })

export const doNotLikeAPI = id =>
  axios({
    url: '/v1_0/article/dislikes',
    method: 'POST',
    data: {
      target: id
    }
  })

export const reportAPI = (id, type) =>
  axios({
    url: '/v1_0/article/reports',
    method: 'POST',
    data: {
      target: id,
      type: '',
      remark: ''
    }
  })

export const setTheChannelAPI = channels =>
  axios({
    url: '/v1_0/user/channels',
    method: 'PUT',
    data: {
      channels
    }
  })

export const deleteTheSpecifiedChannelAPI = id =>
  axios({
    url: `/v1_0/user/channels/${id}`,
    method: 'DELETE'
  })

export const searchSuggestionAPI = q =>
  axios({
    url: '/v1_0/suggestion',
    method: 'GET',
    params: {
      q
    }
  })

export const searchResultsAPI = ({ page, per_page, q }) =>
  axios({
    url: '/v1_0/search',
    method: 'GET',
    params: {
      page,
      per_page,
      q
    }
  })

export const newsDetailsAPI = id =>
  axios({
    url: `/v1_0/articles/${id}`,
    method: 'GET'
  })

//关注
export const focusOnAPI = ({ target }) =>
  axios({
    url: '/v1_0/user/followings',
    method: 'POST',
    data: {
      target
    }
  })

//取消关注
export const obtainACustomsAPI = ({ target }) =>
  axios({
    url: `/v1_0/user/followings/${target}`,
    method: 'DELETE'
  })

//点赞
export const likeAPI = target =>
  axios({
    url: '/v1_0/article/likings',
    method: 'POST',
    data: {
      target
    }
  })

//取消点赞

export const cancelAPI = id =>
  axios({
    url: `/v1_0/article/likings/${id}`,
    method: 'DELETE'
  })

//获取评论
export const getACommentAPI = ({ id, offset = null, limit = 10 }) =>
  axios({
    url: '/v1_0/comments',
    method: 'GET',
    params: {
      type: 'a',
      source: id,
      offset,
      limit
    }
  })

//点赞
export const MyLikeAPI = id =>
  axios({
    url: '/v1_0/comment/likings',
    method: 'POST',
    data: {
      target: id
    }
  })

//取消点赞
export const MyCancelAPI = id =>
  axios({
    url: `/v1_0/comment/likings/${id}`,
    method: 'DELETE'
  })

//添加评论

export const addCommentAPI = ({ id, contentText, art_id = null }) => {
  const obj = {
    target: id,
    content: contentText
  }
  if (art_id !== null) {
    obj.art_id = art_id
  }
  return axios({
    url: '/v1_0/comments',
    method: 'POST',
    data: obj
  })
}

//文章收藏
export const articleCollectionAPI = target =>
  axios({
    url: '/v1_0/article/collections',
    method: 'POST',
    data: {
      target
    }
  })

//取消收藏
export const cancelCollectionAPI = id =>
  axios({
    url: `/v1_0/article/collections/${id}`,
    method: 'DELETE'
  })

//user中心
export const personalCenterAPI = () =>
  axios({
    url: '/v1_0/user/profile',
    method: 'GET'
  })

//用户基本信息
export const basicUserInformationAPI = () =>
  axios({
    url: '/v1_0/user'
  })

//修改头像
export const modifyAvatarAPI = photo =>
  axios({
    url: '/v1_0/user/photo',
    method: 'PATCH',
    data: photo
  })

//修改个人资料
export const modifyPersonalInformationAPI = obj => {
  const NUobj = {
    name: '',
    gender: 0,
    birthday: '',
    intro: ''
  }
  for (const pop in NUobj) {
    if (obj[pop] === undefined) {
      delete NUobj[pop]
    } else {
      NUobj[pop] = obj[pop]
    }
  }
  return axios({
    url: '/v1_0/user/profile',
    method: 'PATCH',
    data: NUobj
  })
}

//token
export const refreshTokenAPI = () => axios({
  url: '/v1_0/authorizations',
  method: 'PUT',
  headers: {
    Authorization: `Bearer ${localStorage.getItem('refresh_token')}`
  }
})